from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from app.api import deps
from app.schemas import hotel
from app.database.models.erp import HotelInfo
from app.database.models.base import User

router = APIRouter()

@router.get("/", response_model=List[hotel.Hotel])
def list_hotels(
    db: Session = Depends(deps.get_db),
    skip: int = 0,
    limit: int = 100,
    current_user: User = Depends(deps.get_current_active_user)
) -> Any:
    """获取酒店列表"""
    hotels = db.query(HotelInfo).offset(skip).limit(limit).all()
    return hotels

@router.post("/", response_model=hotel.Hotel)
def create_hotel(
    *,
    db: Session = Depends(deps.get_db),
    hotel_in: hotel.HotelCreate,
    current_user: User = Depends(deps.get_current_active_user)
) -> Any:
    """创建新酒店"""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Not enough permissions")
    
    # 检查酒店ID是否已存在
    if db.query(HotelInfo).filter(HotelInfo.HotelId == hotel_in.HotelId).first():
        raise HTTPException(
            status_code=400,
            detail="Hotel ID already registered"
        )
    
    hotel = HotelInfo(**hotel_in.dict())
    db.add(hotel)
    db.commit()
    db.refresh(hotel)
    return hotel

@router.get("/{hotel_id}", response_model=hotel.Hotel)
def get_hotel(
    hotel_id: int,
    db: Session = Depends(deps.get_db),
    current_user: User = Depends(deps.get_current_active_user)
) -> Any:
    """获取特定酒店信息"""
    hotel = db.query(HotelInfo).filter(HotelInfo.ID == hotel_id).first()
    if not hotel:
        raise HTTPException(status_code=404, detail="Hotel not found")
    return hotel

@router.put("/{hotel_id}", response_model=hotel.Hotel)
def update_hotel(
    *,
    db: Session = Depends(deps.get_db),
    hotel_id: int,
    hotel_in: hotel.HotelUpdate,
    current_user: User = Depends(deps.get_current_active_user)
) -> Any:
    """更新酒店信息"""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Not enough permissions")
    
    hotel = db.query(HotelInfo).filter(HotelInfo.ID == hotel_id).first()
    if not hotel:
        raise HTTPException(status_code=404, detail="Hotel not found")
    
    # 如果要更新HotelId，检查新ID是否已存在
    if hotel_in.HotelId and hotel_in.HotelId != hotel.HotelId:
        if db.query(HotelInfo).filter(HotelInfo.HotelId == hotel_in.HotelId).first():
            raise HTTPException(
                status_code=400,
                detail="Hotel ID already exists"
            )
    
    for field, value in hotel_in.dict(exclude_unset=True).items():
        setattr(hotel, field, value)
    
    db.add(hotel)
    db.commit()
    db.refresh(hotel)
    return hotel

@router.delete("/{hotel_id}")
def delete_hotel(
    *,
    db: Session = Depends(deps.get_db),
    hotel_id: int,
    current_user: User = Depends(deps.get_current_active_user)
) -> Any:
    """删除酒店"""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Not enough permissions")
    
    hotel = db.query(HotelInfo).filter(HotelInfo.ID == hotel_id).first()
    if not hotel:
        raise HTTPException(status_code=404, detail="Hotel not found")
    
    db.delete(hotel)
    db.commit()
    return {"ok": True}
 